package com.qilu.pe.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qilu.pe.R;
import com.qilu.pe.base.Base2Activity;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.base.Global;
import com.qilu.pe.dao.bean.Msg;
import com.qilu.pe.dao.http.APIRetrofit;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.BaseActivity;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

// 消息列表
public class MsgListActivity extends Base2Activity {
    private RecyclerView rcv_list;

    private MsgListAdpter adapter;
    private List<Msg> msgList = new ArrayList<>();

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_msg_list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rcv_list = findViewById(R.id.rcv_list);

        setHeadTitle("消息列表");

        LinearLayoutManager manager = new LinearLayoutManager(this);
        adapter = new MsgListAdpter(this);
        rcv_list.setLayoutManager(manager);
        rcv_list.setAdapter(adapter);

        rxList.add(reqDataList());
        showProgress();
    }

    private Disposable reqDataList() {
        return APIRetrofit.getDefault().reqMsgList(Global.HEADER, "hehe")
                .compose(RxUtil.<BaseResult2<List<Msg>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<Msg>>>() {
                    @Override
                    public void accept(BaseResult2<List<Msg>> baseResult2) throws Exception {
                        hideProgress();
                        if (baseResult2.isSuccess()) {

                        } else {

                        }
                        msgList = baseResult2.getData();
                        adapter.setDataList(msgList);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                    }
                });
    }

    private class MsgListAdpter extends ListBaseAdapter<Msg> {

        public MsgListAdpter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_msg_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView tv_status = holder.getView(R.id.tv_status);
            if (position == 0 || position == 1) {
                tv_status.setText("已读");
            } else {
                tv_status.setText("");
            }
        }
    }
}